/*
 * Copyright (c) 2006-2021, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2023-5-10      ShiHao       first version
 */

#include <rtthread.h>
#include <rtdevice.h>
#include <board.h>
#include <gpio.h>

#include "cpu_use.h"
#include "communicate_uart.h"
#include "audio.h"
#include "key.h"

#ifdef BSP_USING_UART4
static rt_thread_t Uart4Tx_thread;
static rt_thread_t Uart4Rx_thread;
#define Uart4Tx_thread_pri  24
#define Uart4Rx_thread_pri  24
#endif

#ifdef BSP_USING_UART3
static rt_thread_t Uart3Tx_thread;
static rt_thread_t Uart3Rx_thread;
#define Uart3Tx_thread_pri  24
#define Uart3Rx_thread_pri  8
#endif

static rt_thread_t VcomTx_thread;
static rt_thread_t VcomRx_thread;

#define VcomTx_thread_pri 15
#define VcomRx_thread_pri 14

rt_thread_t Audio_thread;
#define Audio_thread_pri   26

static rt_thread_t Key_thread;
#define Key_thread_pri   26

int main(void)
{
#ifdef BSP_USING_UART3
    Uart3Tx_thread = rt_thread_create("uart3_txTask", uart3_txTask, RT_NULL, 512, Uart3Tx_thread_pri, 10);
    if (Uart3Tx_thread != RT_NULL)
    {
        rt_thread_startup(Uart3Tx_thread);
    }
    else
    {
        rt_kprintf("uart3_txTask thread is failed to create!\r\n");
    }
    Uart3Rx_thread = rt_thread_create("uart3_rxTask", uart3_rxTask, RT_NULL, 1024, Uart3Rx_thread_pri, 10);
    if (Uart3Rx_thread != RT_NULL)
    {
        rt_thread_startup(Uart3Rx_thread);
    }
    else
    {
        rt_kprintf("uart3_rxTask thread is failed to create!\r\n");
    }
#endif

#ifdef BSP_USING_UART4
    Uart4Tx_thread = rt_thread_create("uart4_txTask", uart4_txTask, RT_NULL, 512, Uart4Tx_thread_pri, 10);
    if(Uart4Tx_thread != RT_NULL)
    {
        rt_thread_startup(Uart4Tx_thread);
    }
    else
    {
        rt_kprintf("uart4_txTask thread is failed to create!\r\n");
    }
    Uart4Rx_thread = rt_thread_create("uart4_rxTask", uart4_rxTask, RT_NULL, 1024, Uart4Rx_thread_pri, 10);
    if(Uart4Rx_thread != RT_NULL)
    {
        rt_thread_startup(Uart4Rx_thread);
    }
    else
    {
        rt_kprintf("uart4_rxTask thread is failed to create!\r\n");
    }
#endif

#if 1
    /*-----------------------------------------------------------------*/
    /* Audio */
    Audio_thread = rt_thread_create("Audio", audio_task, RT_NULL, 4096, Audio_thread_pri, 10);
    if (Audio_thread != RT_NULL)
    {
//        rt_thread_startup(Audio_thread);
    }
    else
    {
        rt_kprintf("Audio_thread is failed to create!\r\n");
    }
    /*-----------------------------------------------------------------*/

    /*-----------------------------------------------------------------*/
    /* key */
    Key_thread = rt_thread_create("Key_task", key_task, RT_NULL, 512, Key_thread_pri, 10);
    if (Key_thread != RT_NULL)
    {
        rt_thread_startup(Key_thread);
    }
    else
    {
        rt_kprintf("Key_thread is failed to create!\r\n");
    }
    /*-----------------------------------------------------------------*/
#endif

        VcomTx_thread = rt_thread_create("vcom_txTask", vcom_txTask, RT_NULL, 1024, VcomTx_thread_pri, 10);
        if (VcomTx_thread != RT_NULL)
        {
            rt_thread_startup(VcomTx_thread);
        }
        else
        {
            rt_kprintf("uart3_txTask thread is failed to create!\r\n");
        }
        VcomRx_thread = rt_thread_create("vcom_rxTask", vcom_rxTask, RT_NULL, 1024, VcomRx_thread_pri, 10);
        if (VcomRx_thread != RT_NULL)
        {
            rt_thread_startup(VcomRx_thread);
        }
        else
        {
            rt_kprintf("vcom_rxTask thread is failed to create!\r\n");
        }

    G_ioHandle.low(power_led_status);
    static uint8_t cpu_usage_major, cpu_usage_minor;
    while (1)
    {
        cpu_usage_get(&cpu_usage_major, &cpu_usage_minor);
//        rt_kprintf("CPU USAGE:%u.%u%% \r\n", cpu_usage_major, cpu_usage_minor);

        G_ioHandle.toggle(led_3);
        G_ioHandle.toggle(power_led);

        rt_thread_mdelay(50);
    }
    return 0;
}

